package com.jiyun.utils;




import com.jiyun.entity.day02.Catalog;


import java.util.ArrayList;
import java.util.List;

/*
 * 作者：kai
 * 时间：2023/7/26 15:29
 * 描述：
 **/
public class PermissionHelper {
    public static List<Catalog> buildPermission(List<Catalog> allList){
        //创建最终数据封装List集合
        List<Catalog> trees = new ArrayList<>();
        //遍历集合得到第一层的数据 pid=0
        for (Catalog permission : allList) {
            if (permission.getPid()==0){
                //第一层数据
                permission.setLevel(1);
                trees.add(findChildren(permission,allList));
            }
        }
        return trees ;
    }

    //递归往下继续找节点
    private static Catalog findChildren(Catalog permission, List<Catalog> allList) {
        permission.setChildren(new ArrayList<Catalog>());
        for (Catalog it : allList) {
            if (permission.getId().longValue() ==it.getPid().longValue()){
                int level = permission.getLevel() + 1;
                it.setLevel(level);
                if (permission.getChildren()==null){
                    permission.setChildren(new ArrayList<>());
                }
                //封装上一层数据
                permission.getChildren().add(findChildren(it, allList));
            }
        }
        return permission;
    }
}
